package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Role;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.RoleService;
import com.itwn.comic.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleService roleService;


    //根据姓名查询角色
    @GetMapping("/list")
    public ResponseResult<PageInfo<Role>> list(@RequestParam(value = "size",defaultValue = "5")Integer size,
                                               @RequestParam(value = "now",defaultValue = "1")Integer now,
                                               String name){
        ResponseResult<PageInfo<Role>> result = new ResponseResult<>();
        try {
            PageInfo<Role> pageInfo = roleService.queryRoleByPage(size,now,name);
            if(pageInfo != null){
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }

        return result;
    }
    //添加角色
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Role role){
        ResponseResult<Void> result = new ResponseResult<>();
        role.setInsertTime(new Date());
        try{
            int i = roleService.addRole(role);
            if(i != 0){
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(201);
                result.setMsg("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return  result;
    }
    //修改角色名称
    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Role role){
        ResponseResult<Void> result = new ResponseResult<>();
        role.setUpdateTime(new Date());
        try {
            roleService.modify(role);
            result.setCode(200);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }
    //更新权限
    @PutMapping("/modifyPer")
    public ResponseResult<Void> modify(@RequestBody RolePermissionVo roleVo){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.modifyPer(roleVo);
            result.setCode(200);
            result.setMsg("更新权限成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("更新权限失败");
        }
        return result;
    }
    //删除
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id")Long id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.moveRole(id);
            result.setCode(200);
            result.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return  result;
    }

    @GetMapping("/allRoles")
    public ResponseResult<List<Role>> getAllRoles(){
        ResponseResult<List<Role>> result = new ResponseResult<>();
        try {
            List<Role> roles = this.roleService.queryAllRoles();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(roles);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }
}
